package com.sky.mapper;

import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AddressBookMapper {

    /*
    * 查询地址簿表中是否有数据
    * */
    @Select("select count(0) from address_book")
    Long count();

    /*
    * 根据用户id查询地址簿
    * */
    List<AddressBook> selectAddressByUserId(AddressBook addressBook);

    /*
    * 添加地址
    * */
    @Insert("insert into address_book(user_id, consignee, sex, phone, province_code, province_name, city_code, city_name, district_code, district_name, detail, label, is_default) values " +
            "(#{userId},#{consignee},#{sex},#{phone},#{provinceCode},#{provinceName},#{cityCode},#{cityName},#{districtCode},#{districtName},#{detail},#{label},#{isDefault})")
    void insertAddressBook(AddressBook addressBook);

    /*
    * 根据地址id修改地址
    * */
    void updateAddressBookById(AddressBook addressBook);

    /*
    * 根据地址id修改地址
    * */
    @Delete("delete from address_book where id = #{id}")
    void deleteAddressBookById(Long id);

    /*
    * 将之前的默认地址修改为非默认
    * */
    @Update("update address_book set is_default = 0 where is_default = 1 and user_id = #{userId}")
    void updateCancleDefault(AddressBook addressBook);

    /*
    * 查询默认地址
    * */
    @Select("select * from address_book where is_default = #{isDefault} and user_id = #{userId}")
    AddressBook selectDefaultAddress(AddressBook addressBook);

    /*
    * 根据地址簿id查询地址
    * */
    @Select("select * from address_book where id = #{addressBookId}")
    AddressBook selectAddressById(Long addressBookId);
}
